|
|
Thorsten Froehlich wrote:
> In article <3fdce685@news.povray.org> , Wolfgang Wieser <wwi### [at] gmxde>
> wrote:
>
>> They _are_ less efficient. (Because even temporaries trigger allocatation
>> on the heap instead of the fast stack and because the con/destructors
>> are called all the time increasing and decreasing reference counters,
>> etc.)
>
> Huh??? Nothing happens on the heap at all unless you actually need to
> copy dynamically allocated memory.
>
The point was... (see below)
> And on modern compilers an assignment will
> rarely cause a copy being made. Return value optimisation will take care
> of
> almost all cases in modern compilers. Anyway, this really isn't even
> necessary as most STL containers are designed to minimise assignments and
> copies to cases where it is really necessary.
>
For inline functions this is true. Not so for extern linkage.
The result is lots of inline code which results in larger executables
and eventually even in slower code.
>> But _not_ for real simple types. For "I quickly need a vector", using
>> double v[3] is still faster than anything using operator new or
>> applying a fancy con/destructor.
>
> Sorry, but if you need a local variable, who said you should use new???
>
So, if you need a local variable of just such a type we're talking about,
i.e. a class which simply contains a pointer to an internal class,
then the class will be allocated on the stack (okay, fast) and the
internal class will be allocated using operator new on the heap.
All that happens when you call the constructor and initialize your
class. For the normal use, this is just what you want because when
you pass the class to a function, it is quite fast (just increase
reference counter). But if the object is simply a local var you want
on the stack, then the overhead is considerably.
> Of course if one uses new in a brain-dead manner it is easy to make a
> program slow, but it is like using malloc for local variables in C - just
> something nobody would do.
>
Correct. But if your class behaves that way because you're doing reference
counting (see above), then there is little you can do against that.
Wolfgang
Post a reply to this message
|
|